package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.domain.aggregatemodel.VideoMonitor;
import com.smedi.ismedi.inmail.core.domain.dto.VideoMonitorQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.VideoMonitorRemoteApi;
import com.smedi.ismedi.video.monitor.api.dto.request.MonitorSettingQueryListRequest;
import com.smedi.ismedi.video.monitor.api.dto.request.ProjectMonitorSettingRequest;
import com.smedi.ismedi.video.monitor.api.dto.response.MonitorSettingCountResponse;
import com.smedi.ismedi.video.monitor.api.dto.response.MonitorSettingResponse;
import com.smedi.ismedi.video.monitor.api.feign.RemoteMonitorSettingFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class VideoMonitorRemoteApiImpl implements VideoMonitorRemoteApi {
    private final RemoteMonitorSettingFeignApi remoteMonitorSettingFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<VideoMonitor> getVideoMonitorList(VideoMonitorQuery videoMonitorQuery) {
        MonitorSettingQueryListRequest monitorSettingQueryListRequest = adapterConverter.videoMonitorQueryToMonitorSettingQueryListRequest(videoMonitorQuery);
        ResponseData<List<MonitorSettingResponse>> responseData = remoteMonitorSettingFeignApi.getMonitorSettingList(monitorSettingQueryListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.monitorSettingResponseToMonitorSetting(responseData.getData());
    }

    @Override
    public Integer getMonitorSettingCount(VideoMonitorQuery videoMonitorQuery) {
        MonitorSettingQueryListRequest monitorSettingQueryListRequest = adapterConverter.videoMonitorQueryToMonitorSettingQueryListRequest(videoMonitorQuery);
        ResponseData<Integer> responseData = remoteMonitorSettingFeignApi.getMonitorSettingCount(monitorSettingQueryListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }
    @Override
    public MonitorSettingCountResponse getProjectMonitorSettingCount(List<Long> personIds) {
        ResponseData<MonitorSettingCountResponse> responseData = remoteMonitorSettingFeignApi.getProjectMonitorSettingCount(ProjectMonitorSettingRequest.builder().projectIds(personIds).build());
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }




}
